<template>
	<div class="system-config-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" draggable :close-on-click-modal="false" width="640px">
			<el-form ref="configDialogFormRef" :model="state.configForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="配置名称" prop="name" :rules="[{ required: true, message: '配置名称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.configForm.name" placeholder="配置名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="配置编码" prop="code" :rules="[{ required: true, message: '配置编码不能为空', trigger: 'blur' }]">
							<el-input v-model="state.configForm.code" placeholder="配置编码" clearable :disabled="state.configForm.sysFlag == 1" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="值" prop="value" :rules="[{ required: true, message: '值不能为空', trigger: 'blur' }]">
							<el-input v-model="state.configForm.value" placeholder="值">
								<template #append>
									<el-space :size="10" spacer="|">
										<el-dropdown
											style="color: inherit"
											trigger="click"
											@command="
												(value: string) => {
													state.configForm.value = value;
												}
											"
										>
											<el-button style="margin: 0px -20px 0px -10px; color: inherit"> 选项 </el-button>
											<template #dropdown>
												<el-dropdown-menu>
													<el-dropdown-item command="True"> True </el-dropdown-item>
													<el-dropdown-item command="False"> False </el-dropdown-item>
												</el-dropdown-menu>
											</template>
										</el-dropdown>
									</el-space>
								</template>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="内置参数" prop="sysFlag" :rules="[{ required: true, message: '内置参数不能为空', trigger: 'blur' }]">
							<el-radio-group v-model="state.configForm.sysFlag" :disabled="state.configForm.sysFlag == 1 && state.configForm.id != undefined">
								<el-radio :value="1">是</el-radio>
								<el-radio :value="2">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="分组编码">
							<el-input v-model="state.configForm.groupCode" placeholder="分组编码" clearable :disabled="state.configForm.sysFlag == 1" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.configForm.orderNo" placeholder="排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.configForm.remark" placeholder="请输入备注内容" clearable type="textarea" />
						</el-form-item>
					</el-col>
				</el-row>

			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemConfigDialog">
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { useConfigApi } from '/@/api/config';

// 定义子组件向父组件传值/事件
//const emit = defineEmits(['refresh']);
const emits = defineEmits(['handleQuery']);
// const props = defineProps({
// 	title:String,
// 	orgData:Array<PosForm>,
// });
// 定义变量内容
const configDialogFormRef = ref();
const state = reactive({
	configForm: {} as ConfigForm,
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: ConfigForm) => {
	if (type === 'edit') {
		console.log('rowe:'+JSON.stringify(row));
		state.configForm = row;
		state.dialog.title = '编辑配置';
		state.dialog.submitTxt = '修 改';
	} else {
		state.configForm = row;
		state.dialog.title = '新增配置';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	posDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	// getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery', true);
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	// closeDialog();
	// emit('refresh');
	configDialogFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		if (state.configForm.id != undefined && state.configForm.id > 0) {
			useConfigApi().editConfig(state.configForm).then(async (res: AjaxResult) => {
				if (res.success == true) {
					ElMessage.success('更新成功');
					emits('handleQuery');
					closeDialog();
				}
				else {
					ElMessage.warning('更新失败：' + res.message);
				}
			});
		} else {
			useConfigApi().addConfig(state.configForm).then(async (res: AjaxResult) => {
				if (res.success == true) {
					ElMessage.success('新增成功');
					emits('handleQuery');
					closeDialog();
				}
				else {
					ElMessage.warning('新增失败：' + res.message);
				}
			});
		}
		//closeDialog();
	});
};


// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-pos-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
